//
//  ManualGroupMO.m
//  Groups
//
//  Created by Mathieu on 26/07/06.
//  Copyright 2006 __MyCompanyName__. All rights reserved.
//

#import "ManualGroupMO.h"


@implementation ManualGroupMO

-(NSPredicate *)smartGroupFilterPredicate{
	return nil;
}
- (bool)isManualGroup {
    return YES;
}

-(NSSet *)words{
	NSSet *theWords = [self mutableSetValueForKey:@"words"];
	wordCount = [theWords count];
//	[self setPrimitiveValue:[NSNumber numberWithInt:[theWords count]] forKey:@"wordCount"];
	return theWords;
}

-(int)wordCount{
	return wordCount;
}
/**
Returns the image name for the ManualGroup class.  This method is defined
 on the Group class but each subclass must return their own image name 
 (which must also be a TIFF file in the bundle resources.)
 */

- (NSString *)groupImageName {
    return @"image_group_library";
}

-(NSString *)extraPredicateString{
	return @"Manual Group";
}

/**
Retuns an attributed string with the group name, annotated with the image
 for the group at the beginning of th string using a text attachment.  We 
 cache the image per group so as to not reload the image each time.
 */

- (NSAttributedString *)nameWithImage {
	
    NSString *tmpValue;
    NSMutableAttributedString *result;
    NSImage *groupImage = nil;
    
    // check the cache first... 
    if (cachedNameWithImage != nil) {
        return cachedNameWithImage;
    }
    
    // get the name part of the string
    tmpValue = [self valueForKey:@"name"];
    tmpValue = (tmpValue == nil) ? @"" : tmpValue;
    
    // start with a mutablestring with the name (padding a space at beginning)
    result = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@" %@",tmpValue]];
    
    groupImage = [NSImage imageNamed:[self groupImageName]];
    [groupImage setScalesWhenResized:YES];
    [groupImage setSize:NSMakeSize(14, 14)];
    
    if (groupImage != nil) {
		
        NSFileWrapper *wrapper = nil;
        NSTextAttachment *attachment = nil;
        NSAttributedString *icon = nil;
		
        // need a filewrapper to create an NSTextAttachment
        wrapper = [[NSFileWrapper alloc] init];
		
        // set the icon (this is what'll show up in attributed strings)
        [wrapper setIcon:groupImage];
        
        // you need an attachment to create the attributed string as an RTFd
        attachment = [[NSTextAttachment alloc] initWithFileWrapper:wrapper];
        
        // finally, the attributed string for the icon
        icon = [NSAttributedString attributedStringWithAttachment:attachment];
        [result insertAttributedString:icon atIndex:0];
		
        // cleanup
        [wrapper release];
        [attachment release];	
    }
    
    // set and return the result
    cachedNameWithImage = result;    
    return result;
}

- (void)setName:(NSString *)value  {
	
    [self willChangeValueForKey: @"name"];
    [self setPrimitiveValue: value forKey: @"name"];
    [self didChangeValueForKey: @"name"];
}

-(bool)isThisGroupTheLibrary{
	return NO; //DEFINITELY NO!!
}

-(NSString *)predicateAsString{
	return @"This is a manual group";
}
/**
Mutator to set the name of a group.  We remove the image when the name of 
 the group is being edited (since that cannot be changed by the user), so
 here we simply call the normal Core Data methods to change the name and 
 then reset the cached image and name string.
 */

- (void)setNameWithImage:(NSString *)nameWithImage {
	
    [self willChangeValueForKey: @"nameWithImage"];
	
    [self setName:nameWithImage];
    [cachedNameWithImage release];
    cachedNameWithImage = nil;
    
    [self didChangeValueForKey: @"nameWithImage"];
    
}


@end
